package com.hiultra.archive.controller;

import com.hiultra.archive.domain.ArcDossier;
import com.hiultra.archive.service.IArcDossierService;
import com.hiultra.archive.service.IArcFamilyService;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.constant.UserConstants;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.page.TableDataInfo;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.common.utils.StringUtils;
import com.hiultra.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 卷宗管理Controller
 *
 * @author ls
 * @date 2022-07-28
 *
 */
@Api(tags="卷宗Controller")
@RestController
@RequestMapping("/base/dossier")
public class ArcDossierController extends BaseController {
    @Autowired
    private IArcDossierService arcDossierService;
    @Autowired
    private IArcFamilyService arcFamilyService;

    /**
     * 查询卷宗管理列表
     */
    @ApiOperation(value = "查询卷宗管理列表", notes = "查询卷宗管理列表")
    @PreAuthorize("@ss.hasPermi('base:dossier:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArcDossier arcDossier) {
        startPage();
        List<ArcDossier> list = arcDossierService.selectArcDossierList(arcDossier);
        return getDataTable(list);
    }

    /**
     * 导出卷宗管理列表
     */
    @ApiOperation(value = "导出卷宗管理列表", notes = "导出卷宗管理列表")
    @PreAuthorize("@ss.hasPermi('base:dossier:export')")
    @Log(title = "卷宗管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArcDossier arcDossier) {
        List<ArcDossier> list = arcDossierService.selectArcDossierList(arcDossier);
        ExcelUtil<ArcDossier> util = new ExcelUtil<ArcDossier>(ArcDossier.class);
        util.exportExcel(response, list, "卷宗管理数据");
    }

    /**
     * 获取卷宗管理详细信息
     */
    @ApiOperation(value = "获取卷宗管理详细信息", notes = "获取卷宗管理详细信息")
    @PreAuthorize("@ss.hasPermi('base:dossier:query')")
    @GetMapping(value = {"/", "/{dossierId}"})
    public AjaxResult getInfo(@PathVariable(value = "dossierId", required = false) Long dossierId) {
        AjaxResult ajax = AjaxResult.success();

        ajax.put("familys", arcFamilyService.selectArcFamilyList(null));
        if (StringUtils.isNotNull(dossierId)) {
            ArcDossier arcDossier = arcDossierService.selectArcDossierByDossierId(dossierId);
            ajax.put(AjaxResult.DATA_TAG, arcDossier);
        }

        return ajax;
    }

    /**
     * 新增卷宗管理
     */
    @ApiOperation(value = "新增卷宗管理", notes = "新增卷宗管理")
    @PreAuthorize("@ss.hasPermi('base:dossier:add')")
    @Log(title = "卷宗管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArcDossier arcDossier) {
        if (UserConstants.NOT_UNIQUE.equals(arcDossierService.checkDossierCodeUnique(arcDossier))) {
            return AjaxResult.error("新增案卷'" + arcDossier.getDossierCode() + "'失败，案卷号已存在");
        }
        return toAjax(arcDossierService.insertArcDossier(arcDossier));
    }

    /**
     * 修改卷宗管理
     */
    @ApiOperation(value = "修改卷宗管理", notes = "修改卷宗管理")
    @PreAuthorize("@ss.hasPermi('base:dossier:edit')")
    @Log(title = "卷宗管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArcDossier arcDossier) {
        if (UserConstants.NOT_UNIQUE.equals(arcDossierService.checkDossierCodeUnique(arcDossier))) {
            return AjaxResult.error("修改案卷'" + arcDossier.getDossierCode() + "'失败，案卷号已存在");
        }
        return toAjax(arcDossierService.updateArcDossier(arcDossier));
    }

    /**
     * 删除卷宗管理
     */
    @ApiOperation(value = "删除卷宗管理", notes = "删除卷宗管理")
    @PreAuthorize("@ss.hasPermi('base:dossier:remove')")
    @Log(title = "卷宗管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dossierIds}")
    public AjaxResult remove(@PathVariable Long[] dossierIds) {
        return toAjax(arcDossierService.deleteArcDossierByDossierIds(dossierIds));
    }

    /**
     * 下载卷宗模板
     */
    @ApiOperation(value = "下载卷宗模板", notes = "下载卷宗模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ArcDossier> util = new ExcelUtil<ArcDossier>(ArcDossier.class);
        util.importTemplateExcel(response, "卷宗数据");
    }
    /**
     * 导入卷宗
     */
    @ApiOperation(value = "导入卷宗", notes = "导入卷宗")
    @Log(title = "导入卷宗", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('base:dossier:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ArcDossier> util = new ExcelUtil<ArcDossier>(ArcDossier.class);
        List<ArcDossier> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = arcDossierService.importDossier(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

}
